﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Management;
using System.Collections;
namespace skydream
{
    /// <summary>
    /// 系统服务
    /// </summary>
    public class SystemHelp
    {
        //
        // 摘要:
        //     得到本机的ip地址
        //
        // 返回结果:
        //     返回ip地址
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static string getIp()
        {
            string hostname = System.Net.Dns.GetHostName();
            System.Net.IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(hostname);
            string log_ip = ipEntry.AddressList[0].ToString();
            return log_ip;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static ArrayList GetIPAddress()
        {
            try
            {
                ArrayList strs = new ArrayList();
                //获取IP地址
                string st = "";
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"] == true)
                    {
                        //st=mo["IpAddress"].ToString();
                        System.Array ar;
                        ar = (System.Array)(mo.Properties["IpAddress"].Value);
                        st = ar.GetValue(0).ToString();
                        strs.Add(st);
                        //break;
                    }
                }
                moc = null;
                mc = null;
                return strs;
            }
            catch
            {
                return new ArrayList();
            }
            finally
            {
            }

        }


        /// <summary>
        /// 获取系统的Mac地址
        /// </summary>
        /// <returns></returns>
        public static string getMac()
        {
            string MacAddr = null;
            
            ManagementObjectSearcher Query = new ManagementObjectSearcher(
                "select * from Win32_NetWorkAdapterConfiguration");
            ManagementObjectCollection QureyCollection = Query.Get();
            foreach (ManagementObject mo in QureyCollection)
            {
                if (mo["IPEnabled"].ToString() == "True")
                {
                    MacAddr = mo["MacAddress"].ToString();
                }
            }

            return MacAddr;
        }

        /// <summary>
        /// 获取该系统的所有的MaC地址
        /// 
        /// </summary>
        /// <returns></returns>
        public static ArrayList getMacAll()
        {
            ArrayList alist = new ArrayList();
            string MacAddr = null;

            ManagementObjectSearcher Query = new ManagementObjectSearcher(
                "select * from Win32_NetWorkAdapterConfiguration");
            ManagementObjectCollection QureyCollection = Query.Get();
            foreach (ManagementObject mo in QureyCollection)
            {
                if (mo["IPEnabled"].ToString() == "True")
                {
                    MacAddr = mo["MacAddress"].ToString();
                    alist.Add(MacAddr);
                }
            }

            return alist;
        }
        /// <summary>
        /// cpuID
        /// </summary>
        /// <returns></returns>
        public static string GetCpuID()
        {
            try
            {
                //获取CPU序列号代码
                string cpuInfo = "";//cpu序列号
                ManagementClass mc = new ManagementClass("Win32_Processor");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                }
                moc = null;
                mc = null;
                return cpuInfo;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }

        }
        /// <summary>
        /// 获取硬盘id
        /// </summary>
        /// <returns></returns>
        public static string GetDiskID()
        {
            try
            {
                //获取硬盘ID
                String HDid = "";
                ManagementClass mc = new ManagementClass("Win32_DiskDrive");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    HDid = (string)mo.Properties["Model"].Value;
                }
                moc = null;
                mc = null;
                return HDid;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }

        }
        /// <summary>
        /// 操作系统的登录用户名
        /// </summary>
        /// <returns></returns>
        public static string GetUserName()
        {
            try
            {
                string st = "";
                ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {

                    st = mo["UserName"].ToString();

                }
                moc = null;
                mc = null;
                return st;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }

        }


        /// <summary>
        /// PC类型
        /// </summary>
        /// <returns></returns>
        public static string GetSystemType()
        {
            try
            {
                string st = "";
                ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {

                    st = mo["SystemType"].ToString();

                }
                moc = null;
                mc = null;
                return st;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }

        }

        /// <summary>
        /// 物理内存
        /// </summary>
        /// <returns></returns>
        public static string GetTotalPhysicalMemory()
        {
            try
            {

                string st = "";
                ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {

                    st = mo["TotalPhysicalMemory"].ToString();

                }
                moc = null;
                mc = null;
                return st;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }
        }


    }
}
